package com.gper.edu.vip.builder.singleton;

/**
 * @Author: ellis.guan
 * @Description: 懒汉式双重检查
 * 优点： 线程安全，性能高了
 * 缺点： 可读性难度大，不够优雅
 * @Date: 2020/2/26 21:28
 * @Version: 1.0
 */
public class LazyDoubleCheckSingleton {
    //volatile 防止指令重排序问题
    private volatile static LazyDoubleCheckSingleton singleton;

    private LazyDoubleCheckSingleton(){}

    public static LazyDoubleCheckSingleton getInstance(){
        //第一次检查，检查是否要阻塞，可提高性能
        if (singleton == null){
            synchronized (LazyDoubleCheckSingleton.class){
                //第二检查，检查是否在创建对象
                if (singleton == null) {
                    //指令重排序的问题
                    singleton = new LazyDoubleCheckSingleton();
                }
            }
        }
        return singleton;
    }
}
